package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;

import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //查找所有角色信息
    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //带分页查找角色信息
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        int currentPage = queryPageBean.getCurrentPage();
        int pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);

        Long total = page.getTotal();
        List rows = page.getResult();


        return new PageResult(total, rows);
    }

    //添加新角色
    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //添加新用户

        //添加用户信息
        roleDao.add(role);

        Integer roleId= role.getId();

        //建立角色和权限的关联
        setRoleAndPermission(roleId, permissionIds);

        //建立角色和菜单的关联
        setRoleAndMenu(roleId, menuIds);

    }
    //根据id查询角色信息
    @Override
    public Role findById(Integer roleId) {
        return roleDao.findById(roleId);
    }

    //通过角色id查找关联的权限id
    @Override
    public List<Integer> findPermissionIdsByRoleId(Integer roleId) {
        return roleDao.findPermissionIdsByRoleId(roleId);
    }

    //通过角色id查找关联的菜单id
    @Override
    public List<Integer> findMenuIdsByRoleId(Integer roleId) {
        return roleDao.findMenuIdsByRoleId(roleId);
    }


    //更新角色信息
    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //保存编辑信息
        roleDao.edit(role);
        //删除旧关系
        roleDao.deletePermissionAssoication(role.getId());
        roleDao.deleteMenuAssoication(role.getId());
        //建立新关系
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(), menuIds);
    }

    //根据id删除
    @Override
    public void deleteById(Integer id) {
        //检查删除id是否和角色表有关联
        List<Integer> list =  roleDao.findMenuIdsByRoleId(id);
        List<Integer> list2 =  roleDao.findPermissionIdsByRoleId(id);
        if (!list.isEmpty() || !list2.isEmpty()) {
            throw new RuntimeException("该角色和"+list2.size()+"个权限相关联,和"+list.size()+"个菜单相关联");
        }else {
            roleDao.deleteById(id);
        }
    }

    //建立角色和菜单的关联
    private void setRoleAndMenu(Integer roleId, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

    //建立角色和权限的关联
    private void setRoleAndPermission(Integer roleId, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }
}
